<?php

/**
 * @method class() class($name, $return_instance);
 * @method function() function($name, $return_instance);
 */
class Loader
{
	private $config_loaded = array();
	
	private $class_loaded = array();
	
	private $function_loaded = array();
	
		
	public function __construct()
	{

	}
	
	/**
	 * 	$controller->load->class('template', false);
	 * 	$controller->load->function('common'); 
	 */
	public function __call($name, $params)
	{
		$load_type = $name;
		$return_instance = false;
		if ( ! in_array($load_type, array('class', 'function'))) 
		{
			// in order to mock call to undefined method. funny!
			trigger_error("Call to undefined method Loader::{$load_type}() ", E_USER_ERROR);
		}
		
		if ( ! isset($params[0]))
		{
			// in order to mock call a function without essential params
			trigger_error("Missing argument 1 for Loader::{$load_type}(), called", E_USER_WARNING);
		}
		
		$load_file = $params[0];
		
		if (isset($params[1])) 
		{
			$return_instance = $params[1];
		}
		return $this->library($load_type, $load_file, $return_instance);
	}
	
	private function library($type, $name, $return_instance)
	{
		if ($type == 'class' && in_array($name, $this->class_loaded)) return true;
		if ($type == 'function' && in_array($name, $this->function_loaded)) return true;
			
		$is_sub = false;
		$lib_folder =  ($type == 'class') ? 'classes' : 'functions'; 
		$system_lib_dir = SYSTEM_DIR . $lib_folder . DS;
		$extension_lib_dir = EXTENSION_DIR . $lib_folder . DS;
		$class_name = ucfirst($name);
		
		if (file_exists($system_lib_dir . $name . EXT)) // 系统类库
		{
			$path = $system_lib_dir . $name . EXT;
			if (file_exists($extension_lib_dir . EXTENSION_PREFIX . $name . EXT)) // 继承扩展
			{
				$path = $extension_lib_dir . EXTENSION_PREFIX . $name . EXT;
				$is_sub = true;
			}
		} 
		else if (file_exists($extension_lib_dir . $name . EXT)) // 覆盖扩展
		{
			$path = $extension_lib_dir . $name . EXT;
		} 
		else 
		{
			system_error("未找到扩展文件（{$type}）：" . $name . EXT);
		}
		
		if ($is_sub) $name = EXTENSION_PREFIX . $name;
		
		if ($type == 'class') 
		{
			include_once $path;
			$this->class_loded[] = $name;
			if ($return_instance) return new $class_name;
			$fw = Controller::get_instance();
			$fw->$name = new $class_name;
			return true;
		}
		include_once $path;
		$this->function_loaded[] = $name;
	}
	

	public function view($file, $data = array()) 
	{
		if ( ! file_exists(TEMPLATE_DIR . $file . EXT)) system_error();
		if ($data) extract($data);
		ob_start();
		include TEMPLATE_DIR . $file . EXT;
		$data = ob_get_contents();
		ob_get_clean();
		return $data;
	}
	
	public function config($file = 'config')
	{
		if (in_array($file, $this->config_loaded)) return;
		$fw = Controller::get_instance();
		$data = (array)$fw->config;
		if ( ! file_exists(APPLICATION_DIR . 'config' . DS . $file . EXT)) {
			system_error('未找到配置文件：' . $file . EXT);
		}
		include APPLICATION_DIR . 'config' . DS . $file . EXT;
		$this->config_loaded[] = $file;
		if ( ! isset($config) || ! is_array($config)) system_error();
		$data = array_merge($data, $config);
		unset($config);
		foreach ($data as $k => $v) {
			$data[$k] = $v;
		}
		$fw->config = (object)$data;
	}
	
	public function model($file)
	{
		
	}
}